package 笔试;

public class 蛇形填数 {
    public int[][] snake_matrix (int n) {
        // write code here
        int[][] arr = new int[n][n];
        tian(arr, 0, arr.length-1, 1);

        return arr;
    }

    private static void tian(int[][] arr, int m, int n, int b) {
        for(int i = m; i <= n; i++) {
            arr[i][m] = b;
            b++;
        }
        for(int j = m+1; j <= n; j++) {
            arr[n][j] = b;
            b++;
        }
        for(int i = n-1; i >= m; i--) {
            arr[i][n] = b;
            b++;
        }
        for(int j = n-1; j > m; j--) {
            arr[m][j] = b;
            b++;
        }

        n = n-1;
        m = m+1;
        if(n >= 1) {
            tian(arr, m, n, b);
        }
    }
}
